﻿Shader "Custom/Terrain"
{
    Properties
    {
        // PBR参数
        _SmoothAll("光滑度", Range(0, 1)) = 1
        _Metallic("金属度", Range(0, 1)) = 0.1
        _Occlusion("环境光", Range(0, 10)) = 1

        _Splat1("Splat1", 2D) = "black" {}
        [HDR]_Color_Splat1("Color-Splat1", Color) = (1,1,1,0)
        [NoScaleOffset][Normal] _Splat1N("Splat1-N", 2D) = "bump" {}
        _Smooth1("Smooth1", Range(0 , 2)) = 1
        _Splat1NScale("Splat1-N-Scale", Float) = 1

        _Splat2("Splat2", 2D) = "black" {}
        [HDR]_Color_Splat2("Color-Splat2", Color) = (1,1,1,0)
        [NoScaleOffset][Normal] _Splat2N("Splat2-N", 2D) = "bump" {}
        _Smooth2("Smooth2", Range(0 , 2)) = 1
        _Splat2NScale("Splat2-N-Scale", Float) = 1

        _Splat3("Splat3", 2D) = "black" {}
        [HDR]_Color_Splat3("Color-Splat3", Color) = (1,1,1,0)
        [NoScaleOffset][Normal] _Splat3N("Splat3-N", 2D) = "bump" {}
        _Smooth3("Smooth3", Range(0 , 2)) = 1
        _Splat3NScale("Splat3-N-Scale", Float) = 1

        _Splat4("Splat4", 2D) = "black" {}
        [HDR]_Color_Splat4("Color-Splat4", Color) = (1,1,1,0)
        [NoScaleOffset][Normal] _Splat4N("Splat4-N", 2D) = "bump" {}
        _Smooth4("Smooth4", Range(0 , 2)) = 1
        _Splat4NScale("Splat4-N-Scale", Float) = 1

        [NoScaleOffset]_Control("Control", 2D) = "black" {}
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200

        CGPROGRAM
        // Physically based Standard lighting model, and enable shadows on all light types
        #pragma surface surf Standard fullforwardshadows vertex:vert

        // Use shader model 3.0 target, to get nicer looking lighting
        #pragma target 3.0

        half4 _Splat1_ST;
        half4 _Splat2_ST;
        half4 _Splat3_ST;
        half4 _Splat4_ST;

        sampler2D _Splat1;
        sampler2D _Splat2;
        sampler2D _Splat3;
        sampler2D _Splat4;

        sampler2D _Splat1N;
        sampler2D _Splat2N;
        sampler2D _Splat3N;
        sampler2D _Splat4N;

        sampler2D _Control;

        half4 _Color_Splat1;
        half4 _Color_Splat2;
        half4 _Color_Splat3;
        half4 _Color_Splat4;

        half _Smooth1;
        half _Smooth2;
        half _Smooth3;
        half _Smooth4;

        half _Splat1NScale;
        half _Splat2NScale;
        half _Splat3NScale;
        half _Splat4NScale;

        half4 _ColorAll;

        // 金属度
        half _Metallic;

        // 光滑度
        half _SmoothAll;

        // 环境光
        half _Occlusion;

        struct Input
        {
            float4 texcoord;
        };

        void vert(inout appdata_full v, out Input i)
		{
			UNITY_INITIALIZE_OUTPUT(Input, i);
            i.texcoord = v.texcoord;
        }

        // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
        // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
        // #pragma instancing_options assumeuniformscaling
        UNITY_INSTANCING_BUFFER_START(Props)
        // put more per-instance properties here
        UNITY_INSTANCING_BUFFER_END(Props)

        void surf(Input IN, inout SurfaceOutputStandard o)
        {
            // 读取纹理
            half4 color1 = tex2D(_Splat1, IN.texcoord.xy * _Splat1_ST.xy + _Splat1_ST.zw);
            half4 color2 = tex2D(_Splat2, IN.texcoord.xy * _Splat2_ST.xy + _Splat2_ST.zw);
            half4 color3 = tex2D(_Splat3, IN.texcoord.xy * _Splat3_ST.xy + _Splat3_ST.zw);
            half4 color4 = tex2D(_Splat4, IN.texcoord.xy * _Splat4_ST.xy + _Splat4_ST.zw);

            half4 color1n = tex2D(_Splat1N, IN.texcoord.xy * _Splat1_ST.xy + _Splat1_ST.zw);
            half4 color2n = tex2D(_Splat2N, IN.texcoord.xy * _Splat2_ST.xy + _Splat2_ST.zw);
            half4 color3n = tex2D(_Splat3N, IN.texcoord.xy * _Splat3_ST.xy + _Splat3_ST.zw);
            half4 color4n = tex2D(_Splat4N, IN.texcoord.xy * _Splat4_ST.xy + _Splat4_ST.zw);

            half4 control1 = tex2D(_Control, IN.texcoord.xy);

            color1n.rgb = UnpackNormalWithScale(color1n, _Splat1NScale);
            color2n.rgb = UnpackNormalWithScale(color2n, _Splat2NScale);
            color3n.rgb = UnpackNormalWithScale(color3n, _Splat3NScale);
            color4n.rgb = UnpackNormalWithScale(color4n, _Splat4NScale);

            half3x3 blend = half3x3(half3(control1.r, control1.g, control1.b), half3(control1.a, 0, 0), half3(0, 0, 0));

            // 混合后的颜色
            half4 color = color1 * blend._11 * _Color_Splat1;
            color += color2 * blend._12 * _Color_Splat2;
            color += color3 * blend._13 * _Color_Splat3;
            color += color4 * blend._21 * _Color_Splat4;
            color.a = 1;

            // 法线
            half4 normal = color1n * blend._11;
            normal += color2n * blend._12;
            normal += color3n * blend._13;
            normal += color4n * blend._21;

            o.Albedo = color.rgb;
            o.Normal = normal;
            o.Metallic = _Metallic;
            o.Smoothness = _SmoothAll;
            o.Alpha = 1;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
